<template>
  <div class="order-history">
    <el-table :data="orders" style="width: 100%">
      <el-table-column prop="orderId" label="订单号" width="180" />
      <el-table-column prop="shopName" label="店铺" />
      <el-table-column prop="total" label="金额" width="100">
        <template slot-scope="scope">¥{{ scope.row.total }}</template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120">
        <template slot-scope="scope">
          <el-tag :type="statusType(scope.row.status)">
            {{ statusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="下单时间" width="180">
        <template slot-scope="scope">{{ formatDate(scope.row.createTime) }}</template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button size="mini" @click="viewDetail(scope.row.orderId)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script>
import { formatDate } from '@/utils/date'

export default {
  name: 'OrderHistory',
  data() {
    return {
      orders: [],
      total: 0,
      pageSize: 10,
      currentPage: 1
    }
  },
  methods: {
    formatDate,
    
    statusText(status) {
      const statusMap = ['待支付', '已支付', '已完成', '已取消']
      return statusMap[status] || '未知状态'
    },
    
    statusType(status) {
      const typeMap = ['warning', 'success', '', 'info']
      return typeMap[status] || ''
    },
    
    viewDetail(orderId) {
      console.log('查看订单详情:', orderId)
    },
    
    loadOrders() {
      // 模拟订单数据，实际项目中应该调用API
      const mockOrders = [
        {
          orderId: 'ORD20231201001',
          shopName: '川味小厨',
          total: 68.50,
          status: 1,
          createTime: Date.now() - 86400000 // 1天前
        },
        {
          orderId: 'ORD20231130002',
          shopName: '粤式茶餐厅',
          total: 45.00,
          status: 2,
          createTime: Date.now() - 172800000 // 2天前
        },
        {
          orderId: 'ORD20231129003',
          shopName: '东北饺子馆',
          total: 32.80,
          status: 0,
          createTime: Date.now() - 259200000 // 3天前
        }
      ]
      
      this.orders = mockOrders
      this.total = mockOrders.length
    },
    
    handlePageChange(page) {
      this.currentPage = page
      this.loadOrders()
    }
  },
  
  mounted() {
    this.loadOrders()
  }
}
</script>

<style scoped>
.pagination {
  margin-top: 20px;
  justify-content: center;
}
</style>
